<?php


class Image
{
	var $filepath;					//图片完整路径	e.g. eee/aaa/bbb.jpg
	var $filename;					//图片名			e.g. bbb
	var $filetype;					//图片格式		e.g. jpg
	var $dirname;					//路径			e.g. eee/aaa
	var $quality = 80;				//jpeg质量
	var $big_box_width = 300;		//大缩略图长宽最大值
	var $small_box_width = 90; 		//小缩略图长宽最大值
	var $small_filepath;			//小缩略图地址
	var $big_filepath;				//大缩略图地址
	var $small_water_mark = 'images/small_water_mark.gif';
	var $big_water_mark = 'images/big_water_mark.gif';
	var $max;
	var $md5;
	var $orig_width;
	var $orig_height;
	var $inited = false;
	var $mime;

	
	function image($path=false)
	{
		if ($path) $this->filepath = $path;
	}
	
	function init()
	{
		$arr = explode('.',$this->filepath);
		$this->filetype = $arr[count($arr)-1];
		$this->dirname = dirname($this->filepath).'/';
		$arr = getimagesize($this->filepath);
		$this->orig_width = $arr[0];
		$this->orig_height = $arr[1];
		$this->mime = str_replace("image/","",strtolower($arr['mime']));
		$this->mime == "bmp" && $this->mime = "wbmp";
		$this->inited = true;
	}
	
	function make_big_preview()
	{
		if (!$this->inited) $this->init();
		$this->big_filepath = $this->dirname.$this->md5.'_big.'.$this->filetype;
		if ($this->resize_to_box($this->big_box_width,$this->big_filepath))
			return $this->big_filepath;
		else
			return false;
	}
	
	function make_small_preview()
	{
		if (!$this->inited) $this->init();
		$this->small_filepath = $this->dirname.'/'.$this->md5.'_small.'.$this->filetype;
		if ($this->resize_to_box($this->small_box_width,$this->small_filepath))
			return $this->small_filepath;
		else
			return false;
	}
	
	function mark($w_pos=0,$w_pct=80)
	{
		//$this->water_mark($w_pos,$this->small_filepath,$this->small_water_mark,$w_pct);
		$this->water_mark($w_pos,$this->big_filepath,$this->big_water_mark,$w_pct);
	}
	
	function water_mark($w_pos=0,$source_path,$w_img,$w_pct = 80)
	{
    	$source_info   = getimagesize($source_path);
        $source_width        = $source_info[0];
        $source_height       = $source_info[1];
        switch($source_info[2])
		{
            case 1 :
				$source_img = imagecreatefromgif($source_path);
				break;
            case 2 :
				$source_img = imagecreatefromjpeg($source_path);
				break;
            case 3 :
				$source_img = imagecreatefrompng($source_path);
				break;
        }

        $water_info   = getimagesize($w_img);
        $width        = $water_info[0];
        $height       = $water_info[1];
        switch($water_info[2])
		{
            case 1 :
				$water_img = imagecreatefromgif($w_img);
				break;
            case 2 :
				$water_img = imagecreatefromjpeg($w_img);
				break;
            case 3 :
				$water_img = imagecreatefrompng($w_img);
				break;
            default :
				return;
        }

    	switch($w_pos)
		{
       		case 0:
				$wX = rand(0,($source_width - $width));
            	$wY = rand(0,($source_height - $height));
            	break;
        	case 1:
            	$wX = 5;
            	$wY = 5;
            	break;
        	case 2:
            	$wX = ($source_width - $width) / 2;
            	$wY = 0;
            	break;
        	case 3:
            	$wX = $source_width - $width;
            	$wY = 0;
            	break;
        	case 4:
            	$wX = 0;
            	$wY = $source_height - $height;
            	break;
			case 5:
            	$wX = ($source_width - $width) / 2;
            	$wY = $source_height - $height;
            	break;
			case 6:
				$wX = $source_width - $width -5;
				$wY = $source_height - $height -5;
				break;
        	default:
				$wX = ($source_width - $width) / 2;
				$wY = ($source_height - $height) / 2;
            	break;
    	}
		
    	imagealphablending($source_img, true);
		imagecopymerge($source_img, $water_img, $wX, $wY, 0, 0, $width,$height,$w_pct);
		if ($this->mime == 'jpeg')
			eval('imagejpeg($source_img, "'.$source_path.'",'.$this->quality.');');
		else
			eval('image'.$this->mime.'($source_img, "'.$source_path.'");');
		imagedestroy($water_img);
    	imagedestroy($source_img);
	}
	
	
	function resize_to_box($max,$saveto)
	{
		if (!$this->inited) $this->init();
		if ($max) $this->max = $max;
		if ($this->orig_width < $this->orig_height && $this->orig_height>$this->max)
		{
			$height = $this->max;
			$width = ($this->max / $this->orig_height) * $this->orig_width;
		}
		else if ($this->orig_width>$this->orig_height && $this->orig_width>$this->max)
		{
			$width = $max;
			$height = ($this->max/$this->orig_width)*$this->orig_height;
		}
		else
		{
			return copy($this->filepath,$saveto);
		}
		
		$image_p = imagecreatetruecolor($width, $height);
		eval('$_image = imagecreatefrom'.$this->mime.'("'.$this->filepath.'");');
		imagecopyresampled($image_p, $_image, 0, 0, 0, 0, $width, $height, $this->orig_width, $this->orig_height);

		if ($this->mime == 'jpeg')
			eval('image'.$this->mime.'($image_p, "'.$saveto.'",'.$this->quality.');');
		else
			eval('image'.$this->mime.'($image_p, "'.$saveto.'");');
		imagedestroy($image_p);
		return imagedestroy($_image);
	}
}
?>